package com.processor;

import com.entity.TAuthor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.Date;
import java.util.List;

/**
 * Created by del on 16-5-1.
 */
@Component
public class AuthorProcessor implements PageProcessor{

    private Logger LOG = LoggerFactory.getLogger(AuthorProcessor.class);

    private Site site = Site.me().setRetryTimes(10).setSleepTime(3000);

    public void process(Page page) {
        List<String> urls = page.getHtml().links().regex("http://my.csdn.net/[^/^#]+$").all();
        page.addTargetRequests(urls);
        Boolean isOk = page.getHtml().xpath("//div[@class='error-container']").get() == null;
        String uid = page.getUrl().regex("/[^/^#]+$").get().substring(1);
        String nick = page.getHtml().xpath("//dt[@class='person-nick-name']/span/text()").get();
        String fansAmount = page.getHtml().xpath("//dd[@class='fans_num']/b/text()").get();
        String followerAmount = page.getHtml().xpath("//dd[@class='focus_num']/b/text()").get();
        String visitorAmount = page.getHtml().xpath("//em[@data-bind='times']/text()").get();
        if (isOk) {
            try {
                TAuthor tAuthor = new TAuthor();
                tAuthor.setUid(uid);
                tAuthor.setNick(nick);
                tAuthor.setFansAmount(Integer.valueOf(fansAmount));
                tAuthor.setFollowerAmount(Integer.valueOf(followerAmount));
                tAuthor.setUpdateTime(new Date());
                if (StringUtils.isNotBlank(visitorAmount)) {
                    tAuthor.setVisitorAmount(Integer.valueOf(visitorAmount));
                }
                page.putField("author", tAuthor);
            } catch (Exception e){
                LOG.error(e.getMessage());
            }
        }
    }

    public Site getSite() {
        return site;
    }
}
